package insight.miescor.opp.domain;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.sormula.Database;
import org.sormula.SormulaException;
import org.sormula.Table;
import org.sormula.annotation.Where;
import org.sormula.annotation.Wheres;
import org.sormula.annotation.WhereField;


@Wheres({
    @Where(name="role", whereFields=@WhereField(name="role", comparisonOperator="IN")) 
})

public class Roles {
	private String userid;
	private int id;
	private String role;

	
	
	public Roles() {

	}

	
	public static int generateId(Database database) throws SormulaException {
		NumberGenerator numGen = NumberGenerator.loadNextNum(database,
				"RoleId");

		return numGen.getSeriesCurrentNum();

	}
	
	public Roles(JSONObject roleObj) throws JSONException {
		id = roleObj.has("id") ? Integer.parseInt(roleObj.getString("id")) : 0;
		userid = roleObj.has("userid") ? roleObj.getString("userid") : "";
		role = roleObj.has("role") ? roleObj.getString("role") : "";
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
	public static List<Roles> getUserByRole(Database database,String role) throws SormulaException{
		Table<Roles> roleTbl =database.getTable(Roles.class);
		List<Roles> roleList=roleTbl.selectAllCustom("where role = ? ", role);
		return roleList;
	}
	
	public static List<Roles> getRolesByUser(Database database,String userid) throws SormulaException{
		Table<Roles> roleTbl =database.getTable(Roles.class);
		List<Roles> roleList=roleTbl.selectAllCustom("where userid = ? ", userid);
		return roleList;
	}

	@Override
	public String toString() {
		return "Roles [userid=" + userid + ", id=" + id + ", role=" + role
				+ "]";
	}
	
	
}
